<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>111</title>
</head>
<body>
<div id="msg"></div>
<textarea name="" id="tt" cols="30" rows="10"></textarea>
<button id="btn">发送</button>

<script>
    window.onbeforeunload = function() {
        alert(1)
    }
    const msg = document.getElementById('msg');
    let time = 1;
    // if (!window.WebSocket) {
        const ws = new WebSocket('ws://10.60.6.43:3000', 1);
        ws.onopen = ()=>{
            console.log('服务器连接成功')
            msg.innerHTML = '服务器连接成功'
        }
        ws.onclose = () => {
            console.log('服务器关闭连接')
            msg.innerHTML = '服务器关闭连接'
        };
        ws.onmessage = (e) => {
            var p = document.createElement('p');
            var node = document.createTextNode(e.data);
            p.appendChild(node);
            msg.appendChild(p);
        };
        document.getElementById('btn').addEventListener('click', (e) => {
            var text = document.getElementById('tt').value;
            ws.send(JSON.stringify({msg:text,to:'-2-'}))
            document.getElementById('tt').value = '';
        }, false);

    // }
</script>
</body>
</html>